////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2010 galaxy.org
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

package org.galaxy.richflex.components
{
	import flash.events.Event;
	
	import spark.components.TextInput;
	import spark.events.TextOperationEvent;
	
	/**
	 * This is a text input component that can only input numbers.
	 * A number means a real number.
	 * 
	 * @author Cheng Liang
	 * 
	 * @version 0.0.1
	 */
	public class NumberInput extends TextInput
	{
		// --------------------------------------------------------------------------
		//
		// Properties
		//
		// --------------------------------------------------------------------------
		
		// ------------------------------
		// number
		// ------------------------------
		
		[Bindable("numberChange")]
		/**
		 * @private
		 */
		public function set number(value : Number) : void
		{
			var s : String = value.toString();
			if(text != s) {
				text = s;
				dispatchEvent(new Event("numberChange"));
			}
		}
		
		/**
		 * Sets number as input value.
		 */
		public function get number() : Number
		{
			return Number(text);
		}
		
		// --------------------------------------------------------------------------
		//
		// Constructor
		//
		// --------------------------------------------------------------------------
		
		public function NumberInput()
		{
			super();
			this.restrict = "0-9.\\-";
			this.addEventListener(TextOperationEvent.CHANGE, onTextChange);
		}
		
		// --------------------------------------------------------------------------
		//
		// Events handlers
		//
		// --------------------------------------------------------------------------
		
		/**
		 * @private
		 */
		protected function onTextChange(event : TextOperationEvent) : void
		{
			if(text == "" || text.length == 1) {
				return;
			}
			// ensure "-" at the beginning of text
			if(text.charAt(0) == "-") {
				text = "-" + text.substring(1).replace("-", "");
			}
			// only the first one "."
			var idx : int = text.indexOf(".", 0);
			if(idx >= 0) {
				text = text.substring(0, idx + 1) + text.substring(idx + 1).replace(".", "");
			}
			this.selectRange(text.length, text.length); // TODO cursor after editing always set end even users click mouse to set cursor position 
		}
	}
}